package com.welsy.classroom.command
{
	import com.adobe.cairngorm.control.CairngormEvent;
	import flash.events.Event;
	import mx.controls.Alert;
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	import mx.rpc.events.ResultEvent;
	import com.adobe.cairngorm.commands.ICommand;
	import com.welsy.classroom.business.AddUserDelegate;
	import com.welsy.classroom.event.AddUserEvent;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.classroom.model.ClassModelLocator;
	import com.welsy.usermgt.vo.UserVO;
	
	public class AddUserCommand implements ICommand, IResponder
	{
		
		public function execute(event : CairngormEvent):void {
			var delegate : AddUserDelegate = new AddUserDelegate(this);
			var addUserEvent : AddUserEvent = AddUserEvent(event);
			delegate.addUser(addUserEvent.classId, addUserEvent.userId, addUserEvent.login);
		}
		
		public function result( event : Object) : void {
			var xres :XMLList = XMLList(event.result)
			
			var cNS : Namespace = new Namespace(NamespaceLocator.CLASSROOM_DATATYPES);
			var TAG_ADDUSERRESULT : QName = new QName(cNS, "AddUserResult");
			var TAG_RESULTVALUE : QName = new QName(cNS, "ResultValue");
			var sres : String = xres.descendants(TAG_ADDUSERRESULT).child(TAG_RESULTVALUE);
			if(sres.toLowerCase() != "ok") {
				Alert.show("Errore nella richiesta:\n"+sres, "Errore!");
			} else {
				//Refresh the view for seeing the new users
				var cmodel : ClassModelLocator = ClassModelLocator.getInstance();
				cmodel.refreshClass = true;
			}
		}
		
		public function fault( event : Object ) : void {
			Alert.show("classroom.command.AddUserCommand.fault!");
		}
	}
}